<?php

namespace App\Admin\Controllers;


use App\Models\PlatformIncome;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class PlatformIncomeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('收益记录');
            $income=PlatformIncome::sum('amount');
            $content->description('累计收益：'.$income.'元');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('收益记录');
            $content->description('收益记录');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('收益记录');
            $content->description('收益记录');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(PlatformIncome::class, function (Grid $grid) {
            $grid->disableCreation();
            $grid->disableActions();
            $grid->id('ID')->sortable();
            $grid->in_order_num('单号');
            $grid->description('描述');
            $grid->amount('金额');
            $grid->type('收益来源')->display(function ($data){
                return $data?'推广大使':'书籍交易';
            });
            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PlatformIncome::class, function (Form $form) {

        });
    }
}
